const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    today:'',
    enterTime:null,
    leaveTime:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this ;
    let list = [];
    let date = new Date(); 
    let t = date; 
    // console.log(t.getMonth() + 2)
    // t.setMonth(t.getMonth()+1);
    // console.log(t)

    for(let i = 0 ; i<12 ;i++) {

      

      let first_date = this.getFirstDate(t);

      let last_date = this.getLastDate(t);

      let all_date = this.getAllDate(t);


      // 获取第一天的星期
      let f_d_day = (new Date(first_date)).getDay();
      console.log('f_d_day:' + f_d_day)
      // 空白位置个数
      if (f_d_day > 6) {
        var emp = 0
      } else {
        var emp = f_d_day;
      }

      let dateList = {
        year: date.getFullYear(),
        month: date.getMonth() + 1,
        date: []
      };

      // 生成数组
      for (let i = 1; i <= all_date + emp; i++) {
        if (i <= emp) {
          dateList.date.push({})
        } else {
          dateList.date.push({
            id: Number(((new Date(date.setDate(i - emp))).getFullYear()).toString() + this.formatNum((new Date(date.setDate(i - emp))).getMonth()).toString() + this.formatNum(i - emp)) ,

            num: i - emp,

            date: new Date(date.setDate(i - emp)),

            active:false,

            isEnter:false,

            isLeave:false,

            isPass: ((new Date()) > (Number(new Date(date.setDate(i - emp))) + 24 * 60 * 60 * 1000)),

            isToday: ((new Date()) > (Number(new Date(date.setDate(i - emp))))) && ((new Date()) < (Number(new Date(date.setDate(i - emp)))+24*60*60*1000)),
            timestamp: Number(new Date(date.setDate(i - emp)))
          })
        }
      }

      

      list.push(dateList)

      t.setMonth(t.getMonth() + 1, 1);

    };

    console.log(list)
    this.setData({
      list,
      today: Number(new Date())
    })
    

  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },

  // 获取当月第一天(传入当前时间)
  getFirstDate(t) {
    // let date = new Date(t);
     return  t.setDate(1);
  },

  // 获取当月最后一天
  getLastDate(t) {
    var currentMonth = t.getMonth();
    var nextMonth = ++currentMonth;
    var nextMonthFirstDay = new Date(t.getFullYear(), nextMonth, 1);
    var oneDay = 1000 * 60 * 60 * 24;
    let last_date = nextMonthFirstDay - oneDay;
    return last_date;
  },

  // 获取当月总天数
  getAllDate(t) {
    let first_date =this.getFirstDate(t);
    let last_date = this.getLastDate(t);
    let oneDay = 1000 * 60 * 60 * 24;
    return Math.ceil((last_date - first_date) / oneDay);
  },

  // 选择日期
  handleSelect(e) {
    console.log(e)
    let id = e.currentTarget.dataset.id;
    let list = this.data.list;
    console.log(id);
    console.log(parseInt(this.data.enterTime) > parseInt(id))

    // 选中入住
    if (this.data.enterTime == null) {
      console.log(this.data.enterTime, this.data.leaveTime)

      for (let i = 0; i < list.length; i++) {
        for (let j = 0; j < list[i].date.length; j++) {
          let oj = 'list[' + i + '].date[' + j + ']';
          if (list[i].date[j].id == id && list[i].date[j].isPass==false) {
            
            this.setData({
              enterTime: id,
              [oj + 'isEnter']:true,
            })
          }else {
            // this.setData({
            //   [oj + 'isEnter']: false
            // })
          }

          

        }
      }




    // 选中离店
    } else if (this.data.leaveTime == null){
      if(this.data.enterTime >= id ) {
        return 
      }
      // this.setData({
      console.log(this.data.enterTime, this.data.leaveTime)
      // })

      for (let i = 0; i < list.length; i++) {
        for (let j = 0; j < list[i].date.length; j++) {
          // if (list[i].date[j].id < this.data.) {

          // }

          if (list[i].date[j].id == id) {
            let oj = 'list[' + i + '].date[' + j + ']';
            this.setData({
              leaveTime: id,
              [oj + 'isLeave']: true,

            })
          }
        }
      }



    }else {

      // 重新选择入住
      console.log(this.data.enterTime, this.data.leaveTime)
      for (let i = 0; i < list.length; i++) {

        for (let j = 0; j < list[i].date.length; j++) {
          let oj = 'list[' + i + '].date[' + j + ']';
          if (list[i].date[j].id == this.data.enterTime) {
            this.setData({
              [oj + 'isEnter']: false,
            })
          }
        }
      }


      for (let i = 0; i < list.length; i++) {

        for (let j = 0; j < list[i].date.length; j++) {
          let oj = 'list[' + i + '].date[' + j + ']';
          if (list[i].date[j].id == this.data.leaveTime) {
            this.setData({
              [oj + 'isLeave']: false,
            })
          }
        }
      }

      for (let i = 0; i < list.length; i++) {
        
        for (let j = 0; j < list[i].date.length; j++) {
          let oj = 'list[' + i + '].date[' + j + ']';

          if (list[i].date[j].id == id) {
            
            this.setData({
              enterTime: id,
              leaveTime: null,
              [oj + 'isEnter']: true,
              // [oj + 'active']: true
            })
          }else {
            
            // this.setData({
            //   [oj + 'isEnter']: false,
            //   [oj + 'isLeave']: false,
            // })
          }
        }
      }



    }
    // let enter = e.currentTarget.dataset.timestamp ;

    // for(let i = 0; i <list.length ; i++ ) {
    //   for(let j = 0 ; j < list[i].date.length ;j++) {
    //     if(list[i].date[j].id == id ) {
    //       let oj = 'list['+i+'].date['+j+']';
    //       this.setData({
    //         [oj+'active']:true
    //       })
    //     }
    //   }
    // }

    // list.forEach(item => {
    //   item.date.forEach(date =>{
    //     if(date.id == id ){
    //       console
    //     }
    //   })
    // })
  },
  // 格式化数字
  formatNum(n) {
    if(n>=10) {
      return n
    }else {
      return '0'+n
    }
  },

  // 重置
  reset() {
    console.log('reset')
    let eid = this.data.enterTime;
    let lid = this.data.leaveTime;

    let list = this.data.list

    for(let i = 0 ; i< list.length ; i ++) {
      for (let j = 0; j < list[i].date.length; j++) {
        let oj = 'list[' + i + '].date[' + j + ']';
  
        if (list[i].date[j].id == eid ) {

          this.setData({
            [oj+'isEnter']:false,
            enterTime:null
          })
        } else if (list[i].date[j].id == lid) {

          this.setData({
            [oj + 'isLeave']: false,
            leaveTime: null
          })
        }
        
      }


    }




  }

})